<template>
  <div class="dashboard-editor-container">
    <el-row :gutter="40" class="panel-group" style="margin-top:0px;">
      <el-col :lg="6" :sm="12" :xs="12" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-people">
            <svg-icon class-name="card-panel-icon" icon-class="function-case" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              功能用例总数
            </div>
            <count-to :duration="2600" :end-val="totalCount.fun_test_cases_count" :start-val="0" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :lg="6" :sm="12" :xs="12" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-message">
            <svg-icon class-name="card-panel-icon" icon-class="interface-case" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              接口用例总数
            </div>
            <count-to :duration="3000" :end-val="totalCount.api_test_cases_count" :start-val="0" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :lg="6" :sm="12" :xs="12" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-shopping">
            <svg-icon class-name="card-panel-icon" icon-class="test-suite" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              测试套件总数
            </div>
            <count-to :duration="3200" :end-val="totalCount.test_suite_count" :start-val="0" class="card-panel-num" />
          </div>
        </div>
      </el-col>
      <el-col :lg="6" :sm="12" :xs="12" class="card-panel-col">
        <div class="card-panel">
          <div class="card-panel-icon-wrapper icon-money">
            <svg-icon class-name="card-panel-icon" icon-class="api" />
          </div>
          <div class="card-panel-description">
            <div class="card-panel-text">
              接口总数
            </div>
            <count-to :duration="3200" :end-val="totalCount.fun_api_count" :start-val="0" class="card-panel-num" />
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row style="margin-bottom:10px;">
      <div style="text-align: right">
        <el-button-group>
          <el-button icon="el-icon-arrow-left" size="mini" type="primary" @click="lastWeek">上一周</el-button>
          <el-button size="mini" type="primary" @click="nextWeek">下一周<i class="el-icon-arrow-right el-icon--right" /></el-button>
        </el-button-group>
      </div>
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">
        功能测试用例统计
      </div>
      <BarChart :categories="dateList" :data="funTestCaseCount" height="400px" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">
        接口测试用例统计
      </div>
      <BarChart :categories="dateList" :data="apiTestCaseCount" height="400px" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">
        接口数据统计
      </div>
      <BarChart :categories="dateList" :data="apiCount" height="400px" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">
        执行用例总耗时统计
      </div>
      <LineChart :categories="dateList" :data="funTotalTime" height="300px" />
    </el-row>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">
        执行用例的平均耗时统计
      </div>
      <LineChart :categories="dateList" :data="funAvgTime" height="300px" />
    </el-row>

    <!--    todo: 用例离散度数据不准先注释-->
    <!--    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">-->
    <!--      <div style="color: rgba(0, 0, 0, 0.45); font-size: 16px;">-->
    <!--        执行用例离散度统计-->
    <!--      </div>-->
    <!--      <LineChart :categories="dateList" :data="funVar" height="300px" />-->
    <!--    </el-row>-->
  </div>
</template>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }
    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>

<script>
import CountTo from 'vue-count-to'
import moment from 'moment'
import BarChart from '@/components/Charts/BarChart'
import LineChart from '@/components/Charts/LineChart'
import { getTestCaseCount, getTotalCount } from '@/api/dashboard/dashboard'

export default {
  name: 'DashboardAdmin',
  components: { CountTo, LineChart, BarChart },
  data() {
    return {
      totalCount: {},
      dateList: [],
      apiTestCaseCount: [],
      funTestCaseCount: [],
      funTotalTime: [],
      funAvgTime: [],
      funVar: [],
      apiCount: [],
      startDate: moment().subtract(1, 'weeks').format('YYYY-MM-DD'), // 设置开始日期为一周前
      endDate: moment().subtract(1, 'days').format('YYYY-MM-DD') // 结束日期为当前日期
    }
  },
  created() {
    this.$nextTick(() => {
      this.fetchTotalCount()
      this.fetchTestCaseCount()
    })
  },
  methods: {
    lastWeek() {
      const startOfWeek = moment(this.startDate).subtract(1, 'weeks') // 获取上周的开始日期
      const endOfWeek = moment(startOfWeek).add(6, 'days') // 在开始日期的基础上加上6天得到结束日期
      this.startDate = startOfWeek.format('YYYY-MM-DD')
      this.endDate = endOfWeek.format('YYYY-MM-DD')
      this.fetchTestCaseCount()
    },
    nextWeek() {
      const startOfWeek = moment(this.startDate).add(1, 'weeks')
      const endOfWeek = moment(startOfWeek).add(6, 'days')
      this.startDate = startOfWeek.format('YYYY-MM-DD')
      this.endDate = endOfWeek.format('YYYY-MM-DD')
      this.fetchTestCaseCount()
    },
    fetchTotalCount() {
      getTotalCount({
        projectId: this.$store.getters.projectId
      }).then(response => {
        this.totalCount = response.data
      })
    },
    fetchTestCaseCount() {
      getTestCaseCount({
        projectId: this.$store.getters.projectId,
        startDate: this.startDate,
        endDate: this.endDate
      }).then(response => {
        this.dateList = response.data.dateList
        this.apiTestCaseCount = response.data.apiSeries
        this.funTestCaseCount = response.data.funSeries
        this.funTotalTime = response.data.totalTimeSeries
        this.funAvgTime = response.data.avgTimeSeries
        this.funVar = response.data.varSeries
        this.apiCount = response.data.apiCountSeries
      })
    }
  }
}
</script>
